package com.deng.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class ToolUtils {


    /**
     * 身份证里生日和年龄解析
     *
     * @param card
     * @return
     */
    public static Map <String, Object> cardChange(String card) {
        Map <String, Object> map = new HashMap <>();
        Pattern idNumPattern = Pattern.compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
        if (!idNumPattern.matcher(card).matches()) {
            System.err.println("身份证格式不正确");
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String birth = card.substring(6, 10) + "-" + card.substring(10, 12) + "-" + card.substring(12, 14);
        try {
            Integer today = Integer.valueOf(sdf.format(new Date()).substring(0, 4));
            Integer age = Integer.valueOf(birth.substring(0, 4));
            Date birthday = sdf.parse(birth);
            map.put("age", today - age);
            map.put("birthday", birthday);
            return map;
        } catch (ParseException e) {

        }
        return null;
    }

}